#! /bin/bash
#
# Get all slave source from svn
#

ECHO="/bin/echo"
PRINTF="/usr/bin/printf"
MAKE="/usr/bin/make"
MAKE_LOG_FILE="make_log.txt"
SVN="svn --non-interactive"

MINI_UBOOT_DIR=mini_uboot
SOURCE_TOP_DIR=./

top_version=-1
svn_url=invalid_svn_url
get_uboot_source_version()
{
	top_version=`${SVN} info ${SOURCE_TOP_DIR} 2>/dev/null | grep '^Revision'|awk '{print $NF}'`
}

get_uboot_svn_trunk_url()
{
	svn_url=`${SVN} info ${SOURCE_TOP_DIR} 2>/dev/null | grep '^URL'|awk '{print $NF}'`
}

g_last_changed_rev=0
# $1 : folder to check
get_last_changed_rev()
{
	g_last_changed_rev=`${SVN} info $1 2>/dev/null | grep '^Last Changed Rev:'|awk '{print $NF}'`
}

# $1 : To which local folder
folder_need_update()
{
    folder_tgt=$1
    #folder_src=`${ECHO} $1| sed -e 's/${MINI_UBOOT_DIR}//'`
    folder_src=${1/${MINI_UBOOT_DIR}/.}
    get_last_changed_rev ${folder_tgt}
    last_changed_rev_tgt=${g_last_changed_rev}
    get_last_changed_rev ${folder_src}
    last_changed_rev_src=${g_last_changed_rev}

    ${ECHO} "Compare ${folder_tgt}@${last_changed_rev_tgt} ${folder_src}@${last_changed_rev_src}"
    if [[ ${last_changed_rev_src} == ${last_changed_rev_tgt} ]]
    then
        ${ECHO} "no need to udpate"
        return 0
    fi
    return 1
}

# $1 : From which svn URL
# $2 : To which local folder
get_source_code_by_path()
{
	to_folder=$2
	from_url=$1
	${ECHO} "Get source code from" $1 "to " $2
	if [[ -d ${to_folder} ]]
	then
	    ${ECHO} "Folder exist, see if need to update"
        folder_need_update $2 
        if [[ $? == 1 ]]
        then
            ${SVN} update $2 -r ${top_version}
        fi
	else
	    ${ECHO} "Folder not exist, checkout "
	    ${SVN} co $1 $2 -r ${top_version}
	fi
}

get_uboot_source_version
get_uboot_svn_trunk_url
${ECHO} "Uboot software URL" ${svn_url}
${ECHO} "Uboot software version" ${top_version}

# uboot
get_source_code_by_path ${svn_url}/firmware/uboot/  ${SOURCE_TOP_DIR}/firmware/${MINI_UBOOT_DIR}/

# vim:et:sw=4:ts=4

